package request

import (
	"boticz/app/utils/response"

	"github.com/gin-gonic/gin"
)

const (
	Password    GrantTypeEnum = 0
	CaptchaCode GrantTypeEnum = 1
	SmsCode     GrantTypeEnum = 2
	ScanCode    GrantTypeEnum = 3
)

type GrantTypeEnum int

type RqUserLogin struct {
	Pass      string        `json:"pass"`                          // 密码
	GrantType GrantTypeEnum `json:"grant_type" binding:"required"` // 授权方式 密码登录 0 验证码登录 1 短信验证码登录 2 Web扫码登录 3
	UserName  string        `json:"user_name"`                     // 用户名
	Phone     string        `json:"phone"`                         // 手机号码
	Code      string        `json:"code"`                          // 验证码
	Token     string        `json:"token"`                         // 临时Token 用途：扫码登录
}

type LoginWithPasswordType struct {
	Pass     string `json:"pass" binding:"required"`
	UserName string `json:"user_name" binding:"required"`
}

type LoginWithCaptchaCodeType struct {
	Pass     string `json:"pass" binding:"required"`
	UserName string `json:"user_name" binding:"required"`
	Code     string `json:"code" binding:"required"`
}

type LoginWithSmsCodeType struct {
	Phone string `json:"phone" binding:"required"`
	Code  string `json:"code" binding:"required"`
}

type LoginWithScanCodeType struct {
	Token string `json:"token" binding:"required"`
	Code  string `json:"code" binding:"required"`
}

type RqSendCode struct {
	Phone string `json:"phone" binding:"required"` // 手机号码
}

type RqScanCode struct {
	Code string `json:"code" binding:"required"` // 二维码
}

func (o *RqUserLogin) Validate(context *gin.Context) bool {
	if err := context.ShouldBindBodyWithJSON(&o); err != nil {
		response.ValidatorError(context, err)
		return false
	}

	if o.GrantType == Password {
		var passLogin *LoginWithPasswordType
		// 校验用户名和密码
		if err := context.ShouldBindBodyWithJSON(&passLogin); err != nil {
			response.ValidatorError(context, err)
			return false
		}
	}
	if o.GrantType == CaptchaCode {
		var captchaCodeLogin *LoginWithCaptchaCodeType
		// 校验用户名和密码和验证码
		if err := context.ShouldBindBodyWithJSON(&captchaCodeLogin); err != nil {
			response.ValidatorError(context, err)
			return false
		}
	}

	if o.GrantType == SmsCode {
		var smsCodeLogin *LoginWithSmsCodeType
		// 校验手机号码和验证码
		if err := context.ShouldBindBodyWithJSON(&smsCodeLogin); err != nil {
			response.ValidatorError(context, err)
			return false
		}
	}

	if o.GrantType == ScanCode {
		var scanCodeLogin *LoginWithSmsCodeType
		// 校验手机号码和验证码
		if err := context.ShouldBindBodyWithJSON(&scanCodeLogin); err != nil {
			response.ValidatorError(context, err)
			return false
		}
	}

	return true
}

func (o *RqSendCode) Validate(context *gin.Context) bool {
	if err := context.ShouldBindBodyWithJSON(&o); err != nil {
		response.ValidatorError(context, err)
		return false
	}
	return true
}

func (o *RqScanCode) Validate(context *gin.Context) bool {
	if err := context.ShouldBind(&o); err != nil {
		response.ValidatorError(context, err)
		return false
	}
	return true
}
